﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

namespace S3_Sync_Backup
{
    public class S3Client
    {
        protected AmazonS3 client;

        public S3Client(String awsAccessKey, String awsSecretKey)
        {
            this.client = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey);
        }

        public S3Client(String awsAccessKey, String awsSecretKey, AmazonS3Config config)
        {
            this.client = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey, config);
        }

        public List<S3Bucket> ListBuckets()
        {
            try
            {
                ListBucketsResponse response = this.client.ListBuckets();

                return response.Buckets;
            }
            catch (AmazonS3Exception exception)
            {
                throw exception;
            }
        }

        public void CreateBucket(String bucketName, S3Region bucketRegion)
        {
            try
            {
                PutBucketRequest putBucketRequest = new PutBucketRequest();
                putBucketRequest.BucketName = bucketName;
                putBucketRequest.BucketRegion = bucketRegion;

                PutBucketResponse putBucketResponse = this.client.PutBucket(putBucketRequest);
            }
            catch (AmazonS3Exception exception)
            {
                throw exception;
            }
        }

        public List<S3Object> ListFiles(String bucketName)
        {
            try
            {
                ListObjectsRequest listObjectRequest = new ListObjectsRequest();
                listObjectRequest.BucketName = bucketName;

                ListObjectsResponse listObjectResponse = this.client.ListObjects(listObjectRequest);
                return listObjectResponse.S3Objects;
            }
            catch (AmazonS3Exception exception)
            {
                throw exception;
            }
        }

        public void UploadFile(String bucketName, String sourceFile)
        {
            try
            {
                PutObjectRequest putObjectRequest = new PutObjectRequest();
                putObjectRequest.BucketName = bucketName;
                putObjectRequest.FilePath = sourceFile;
                putObjectRequest.Key = Path.GetFileName(sourceFile);

                PutObjectResponse putObjectResonse = this.client.PutObject(putObjectRequest);
            }
            catch (AmazonS3Exception exception)
            {
                throw exception;
            }
        }

        public void DownloadFile(String bucketName, String key)
        {
            try
            {
                GetObjectRequest getObjectRequest = new GetObjectRequest();
                getObjectRequest.BucketName = bucketName;
                getObjectRequest.Key = key;

                GetObjectResponse getObjectResponse = this.client.GetObject(getObjectRequest);

                string dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), getObjectResponse.Key);

                getObjectResponse.WriteResponseStreamToFile(dest);

            }
            catch (AmazonS3Exception exception)
            {
                throw exception;
            }
        }
    }
}
